/**
 *
 */
package org.osite.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class FileResourceLoader implements ResourceLoader {
    private final File root;

    public FileResourceLoader(File root) {
        Preconditions.checkArgument(root.isDirectory(), 
                "not a directory: " + root.getAbsolutePath());
        this.root = root;
    }

    @Override
    public InputStream getResource(String path) {
        try {
            return new FileInputStream(new File(root, path));
        } catch (FileNotFoundException e) {
            return null;
        }
    }

    @Override
    public String[] listResources(String path) {
        File file = new File(path);
        String[] result = file.list();
        for (int i = 0, n = result.length; i < n; ++i) {
            result[i] = path + File.separatorChar + result[i];
        }
        return result;
    }
}